package com.base.testmvvm

import android.arch.lifecycle.Lifecycle
import android.arch.lifecycle.LifecycleOwner
import android.arch.lifecycle.LifecycleRegistry
import android.arch.lifecycle.Observer
import android.arch.lifecycle.ViewModelProviders
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity(),LifecycleOwner {

    var mLifecycleRegistry = LifecycleRegistry(this)

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        var owner = ActivityLifeObserver()
        mLifecycleRegistry.addObserver(owner)



        var mUserViewModel = ViewModelProviders.of(this).get(UserViewModel().javaClass)

        mUserViewModel.data.observe(this, Observer<User> {
            if (it != null)
                textView_content.text = it.toString()
        })

        text_center.setOnClickListener(View.OnClickListener {
            if (mUserViewModel != null && mUserViewModel.data != null) {
                mUserViewModel.changeData()
            }
        })
    }


    override fun getLifecycle(): Lifecycle {
        return mLifecycleRegistry
    }
}
